package com.sduept.nwld.dataserver.manager.message;

import com.sduept.component.message.model.MessageTypeDTO;
import com.sduept.component.message.model.PersonnelConfigure;
import com.sduept.component.message.model.PersonnelMsgConfigureDTO;
import com.sduept.message.entity.MessageReceiveConfig;
import com.sduept.message.entity.MessageType;
import com.sduept.message.manager.MessageReceiveConfigManager;
import com.sduept.message.manager.MessageTypeManager;
import com.sduept.nwld.dataserver.model.library.MessageReceiver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Author
 * @version V1.0
 * @Date 2018年8月15日 下午5:08:25
 */
@Service
public class PersonnelMsgConfigureDTOManager {

	@Autowired
	private MessageTypeManager messageTypeManager;

	@Autowired
	private MessageReceiverManager messageReceiverManager;

	@Autowired
	private PersonnelMessageConfigureManager personnelMessageConfigureManager;

	@Autowired
	private MessageReceiveConfigManager messageRelationManager;

	/**
	 * 根据配置的Id查询
	 * 
	 * @param configureId
	 * @return
	 */
	public PersonnelMsgConfigureDTO getByConfigureId(String configureId) {
		PersonnelMsgConfigureDTO result = new PersonnelMsgConfigureDTO();
		PersonnelConfigure configure = personnelMessageConfigureManager.getByCode(configureId);
		result.setConfigure(configure);
		// 根据 配置id（人员code） 查询t_msg_receive_config
		List<MessageReceiveConfig> personConfigList = messageRelationManager.listByConfigureId(configureId);
		// Map<messageTypeId,msgSenderId集合>
		Map<String, Set<String>> typeReceiverMap = new HashMap<>();
		// 1.查询全部的消息接收类型type
		List<MessageType> allMessageTypes = messageTypeManager.findAll();
		List<MessageTypeDTO> dtos = new ArrayList<>();
		// 2.for循环全部的type
		for (MessageType m : allMessageTypes) {
			MessageTypeDTO dto = new MessageTypeDTO();
			// 3.for循环全部personConfigList
			for (MessageReceiveConfig config : personConfigList) {
				// 4.如果type.id=personConfigList.msgTypeId;查找typeReceiverMap中是不是有这个typeid
				if (m.getId().equals(config.getMsgTypeId())) {
					// 5.如果没有，新建立一个set，添加进这个Map中
					if (!typeReceiverMap.containsKey(m.getId())) {
						Set<String> recSet = new HashSet<>();
						recSet.add(config.getMsgSenderId());
						typeReceiverMap.put(m.getId(), recSet);
					} else {
						// 6.如果有，改变里面的set
						Set<String> recSet = typeReceiverMap.get(m.getId());
						recSet.add(config.getMsgSenderId());
						typeReceiverMap.put(m.getId(), recSet);
					}
				}
			}
			dto.setMsgType(m);
			Set<String> recSet = typeReceiverMap.get(m.getId());
			if (recSet != null && recSet.size() > 0) {
				dto.setReceivers((messageReceiverManager.listByIds(recSet.toArray(new String[0]))));
			} else {
				dto.setReceivers(new ArrayList<>());
			}
			dtos.add(dto);
		}
		result.setConfigure(configure);
		result.setDtos(dtos);
		return result;
	}

	/**
	 * 过滤出符合条件的消息接收方式
	 * 
	 * @param idSet
	 * @param list
	 * @return
	 */
	private List<MessageReceiver> filter(Set<String> idSet, List<MessageReceiver> list) {
		List<MessageReceiver> result = new ArrayList<>();
		for (MessageReceiver receiver : list) {
			if (idSet.contains(receiver.getId())) {
				result.add(receiver);
			}
		}
		return result;
	}

}
